

#include "gui.h"
#include "../app_includes.h"

static lv_obj_t *ui_screen;
static lv_obj_t *ui_LabelVBus;
static lv_obj_t *ui_LabelVo;
static lv_obj_t *ui_btn;
static lv_obj_t *ui_LabelUSB;
//static lv_obj_t *ui_LabelBtn;
static lv_timer_t *lv_timer;

static void btn_homeback_click(lv_event_t *e);
static void btn_event_click(lv_event_t *e);
static void value_refresh(lv_timer_t *timer);

//状态栏入口
lv_obj_t *lv_status_bar_load(void)
{
    //ui_screen = lv_screen_create(ui_main_screen.status_bar,lv_pct(100), lv_pct(100));
    ui_screen = ui_main_screen.status_bar;

    ui_LabelUSB = lv_label_create(ui_screen);
    lv_obj_align(ui_LabelUSB, LV_ALIGN_LEFT_MID, 10, 0);
    lv_label_set_text(ui_LabelUSB, LV_SYMBOL_USB);
    lv_obj_set_style_text_color(ui_LabelUSB, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_font(ui_LabelUSB, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_LabelUSB, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelVBus = lv_label_create(ui_screen);
    lv_obj_align_to(ui_LabelVBus, ui_LabelUSB, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    //lv_label_set_text(ui_LabelVBus, "  20.0V  ");
    lv_obj_set_style_text_color(ui_LabelVBus, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelVBus, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelVBus, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelVBus, 10, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_LabelVBus, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_LabelVBus, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelVo = lv_label_create(ui_screen);
    lv_obj_align(ui_LabelVo, LV_ALIGN_RIGHT_MID, -30, 0);
    //lv_label_set_text(ui_LabelVo, "  20.0V  ");
    lv_obj_set_style_text_color(ui_LabelVo, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelVo, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelVo, &lv_font_montserrat_12, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelVo, 10, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_LabelVo, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_LabelVo, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_btn = lv_btn_create(ui_screen);
    lv_obj_set_x(ui_btn, 0);
    lv_obj_set_y(ui_btn, 0);
    lv_obj_set_size(ui_btn, 20, 20);
    lv_obj_add_event_cb(ui_btn, btn_event_click, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_btn, btn_event_click, LV_EVENT_LONG_PRESSED, NULL);
    lv_group_add_obj(g_button,ui_btn);
    lv_obj_set_style_opa(ui_btn, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    lv_timer = lv_timer_create(value_refresh, 300, NULL);
    
    return ui_screen;
}

static void value_refresh(lv_timer_t *timer)
{
    char str[16] = " VBUS ";
    float_to_str(str+6, app_dev.meter.power_v.value , 1, 4, "V ");  
    lv_label_set_text(ui_LabelVBus,str);
    
    strcpy(str, " VOUT ");
    float_to_str(str+6, app_dev.meter.power_out_v.value , 1, 4, "V ");  
    lv_label_set_text(ui_LabelVo,str);
    if(app_dev.para.status.power_work==0) //
        lv_obj_set_style_bg_color(ui_LabelVo, lv_color_hex(0x292831), LV_PART_MAIN | LV_STATE_DEFAULT );
    else
        lv_obj_set_style_bg_color(ui_LabelVo, lv_color_hex(0x65EBA4), LV_PART_MAIN | LV_STATE_DEFAULT );
        
    
    if(app_pt.cc>0)
        lv_obj_set_style_text_opa(ui_LabelUSB, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    else
        lv_obj_set_style_text_opa(ui_LabelUSB, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    if(xEventGroupWaitBits(sys_event,kSysEvent_PowerOV,pdTRUE,pdFALSE,0)&kSysEvent_PowerOV) {
        msg_show_auto_close(LV_SYMBOL_WARNING, "完啦, 输出过载啦!",2000); //弹出过载消息框
    }
}


static void tabview_select_next_act(lv_obj_t *tabview)
{
    uint8_t num, id;
    if(tabview==NULL)
        return;
    num = ((lv_tabview_t *)tabview)->tab_cnt;
    id = lv_tabview_get_tab_act(tabview);
    id++;
    if(id==num) id = 0;
    lv_tabview_set_act(tabview, id, LV_ANIM_ON);
    lv_group_set_editing(g_encoder, 0);
    lv_group_remove_all_objs(g_encoder);
    if(ui_main_screen.group_select!=NULL) {
        ui_main_screen.group_select(id);
    }
}

static void btn_event_click(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    
    if(ui_main_screen.home_flag==1) { //in home screen
        return;
    }
    else{
        if(code == LV_EVENT_LONG_PRESSED) {
            ui_main_screen.btn_disable = 0;
            btn_homeback_click(NULL);
        }
        else if(code == LV_EVENT_CLICKED && ui_main_screen.btn_disable==0) {
            if(lv_group_get_editing(g_encoder)==true) //处于编辑状态先退出编辑
                lv_group_set_editing(g_encoder, 0);
            else
                tabview_select_next_act(ui_main_screen.app_tabview);
        }
    }
}

static void btn_homeback_click(lv_event_t *e)
{
    lv_group_set_editing(g_encoder, 0);
    lv_group_remove_all_objs(g_encoder);
    lv_obj_del(ui_main_screen.current_desktop);
    ui_main_screen.app_tabview = NULL;
    if(ui_main_screen.app_close!=NULL) {
        ui_main_screen.app_close();
        ui_main_screen.app_close = NULL;
    }
    ui_main_screen.current_desktop = lv_homepage_load();
}

void lv_status_bar_hidden(uint8_t hide)
{
    if(hide) {
        lv_obj_set_style_opa(ui_main_screen.status_bar, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    } else {
        lv_obj_set_style_opa(ui_main_screen.status_bar, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    }

}